//
//  DessertCollectionViewController.m
//  吃货·上海
//
//  Created by lanou on 15/11/24.
//  Copyright © 2015年 濮亚军. All rights reserved.
//

#import "DessertCollectionViewController.h"
#import "PrefixHeader.pch"
#import "DessertModel.h"
#import "DessertCollectionViewCell.h"
#import "DessertDetailTableViewController.h"
#define KUrl "http://221.228.82.181/napi/blog/list/by_category/?include_fields=sender%2Calbum%2Cicon_url%2Creply_count%2Clike_count&platform_version=4.4.4&path=&device_platform=Coolpad%2B8297-T01&__dtac=%257B%2522_r%2522%253A%2520%2522976483%2522%257D&screen_width=720&screen_height=1280&__domain=www.duitang.com&app_version=64&start=0&platform_name=Android&locale=zh&app_code=nayutas&cate_key=54589217586df56fb172b4c5"


@interface DessertCollectionViewController ()<UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UIImageView *icon;//菜谱图
@property(nonatomic,strong)UILabel *infoLable;//菜谱简介
@property(nonatomic,strong)UILabel *replyLable;//评论
@property(nonatomic,strong)UILabel *likeLable;//点赞
@property(nonatomic,strong)UILabel *favouriteLable;//喜欢
@property(nonatomic,strong)NSMutableArray *array;

@end

@implementation DessertCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

//懒加载
- (NSMutableArray *)array
{
    if (! _array) {
        self.array = [[NSMutableArray alloc]init];
    }
    return _array;
}


- (void)loadView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    CGFloat width = (KScreenWidth - 30) / 2;
    layout.itemSize = CGSizeMake(width, width * 2);
    layout.minimumLineSpacing = 10;
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView = collectionView;
    collectionView.backgroundColor = [UIColor whiteColor];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight - 64);
    
    // Register cell classes
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    NSString *url = @KUrl;
    [[[NSURLSession sharedSession]dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSDictionary *dataDic = dic[@"data"];
        for (NSDictionary *listDic in dataDic[@"object_list"])
        {
            DessertModel *model = [[DessertModel alloc]init];
            NSDictionary *photoDic = listDic[@"photo"];
            NSString *path = photoDic[@"path"];
            NSString *pic = [path substringToIndex:[path length]-5];
            model.path = pic;
            NSDictionary *senderDic = listDic[@"sender"];
            [model setValuesForKeysWithDictionary:listDic];
            [model setValuesForKeysWithDictionary:senderDic];
            [self.array addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
    }]resume];
}



#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView registerClass:[DessertCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    DessertCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.model = self.array[indexPath.item];
    cell.backgroundColor = KMyColor;
    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{
    DessertDetailTableViewController *detail = [[DessertDetailTableViewController alloc]init];
    detail.model = self.array[indexPath.item];
    [self.navigationController pushViewController:detail animated:NO];
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}


@end
